package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.domian.statistics.SDKRate;
import org.jsbd.boss.service.ISdkRateService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sdk/rate")
public class SDKRateController {

	private static final Logger logger = LoggerFactory.getLogger(SDKRateController.class);

	@Resource
	private ISdkRateService sdkRateService;

	/**
	 * SDK使用率页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list() {
		return "sdkRate/sdkRateList";
	}

  	/**
  	 * 新增用户SDK利用率
  	 * 
  	 * @param model
  	 * @param startTime
  	 * @return
  	 */
  	@RequestMapping(value = "/listData", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> sdkUseRateData(Model model, String startTime, String endTime) {
  		Map<String, Object> map = new HashMap<String, Object>();
  		String begin = "";
  		String end = "";
  		
  		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
  			begin = DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YYMMDD, 30);
  			end = DateUtil.getCurrentDateFormatStr(DateUtil.DATE_PATTEN_YYMMDD);
  		}
  		
  		if (!StringUtils.isBlank(startTime)) {
  			begin = startTime.replaceAll("-", "");
  		}
  		if (!StringUtils.isBlank(endTime)) {
  			end = endTime.replaceAll("-", "");
  		}
  		
  		List<String> xAxis = sdkRateService.getSdkRateXaxis(begin, end);
  		List<SDKRate> yAxis = sdkRateService.getSdkRateYaxis(begin, end);
  		
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		
		return map;
	}

}
